package com.cloudcc.mobile.dao.impl;

import com.cloudcc.cloudframe.model.param.UserInfoParam;
import com.cloudcc.cloudframe.net.RequestListener;
import com.cloudcc.cloudframe.net.ResultCallBack;
import com.cloudcc.cloudframe.net.async.BeanRequest;
import com.cloudcc.cloudframe.net.async.EventRequest;
import com.cloudcc.cloudframe.net.async.JsonObjectRequest;
import com.cloudcc.mobile.dao.BaseEngine;
import com.cloudcc.mobile.dao.ContactEngine;
import com.cloudcc.mobile.entity.ChatUser;
import com.cloudcc.mobile.entity.CoworkerEntity;
import com.cloudcc.mobile.entity.OperateFollowRelation;
import com.cloudcc.mobile.entity.User;
import com.cloudcc.mobile.event.CoworkerEventList.CoworkerListEvent;
import com.cloudcc.mobile.manager.RunTimeManager;
import com.cloudcc.mobile.manager.UrlManager;
import com.litesuits.android.async.Log;
import com.loopj.android.http.RequestParams;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

public class ContactEngineImpl extends BaseEngine implements ContactEngine {

	@Override
	public void getContacts(ResultCallBack<List<CoworkerEntity>> listener) {
		String px = "isusing = '1'";
		String a = px;
		try {
			a = URLEncoder.encode(px, "utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		String url = UrlManager.getInterfaceUrl()
				+ "?serviceName=cquery&objectApiName=ccuser&expressions=" + a
				+ "&binding=" + RunTimeManager.getInstance().getServerBinding();
		BeanRequest<List<CoworkerEntity>> request = new BeanRequest<List<CoworkerEntity>>() {
		};
		Log.d("---url",url);
		request.setResultListener(listener);
		sendGet(url, request);
	}

	@Override
	public void getContactsSync(ResultCallBack<List<CoworkerEntity>> listener) {
		RequestParams params = new RequestParams();
		params.put("serviceName", "pageQuery");
		params.put("pageNUM", 1);
		params.put("pageSize", 800);
		params.put("objectApiName", "ccuser");
		params.put("binding", RunTimeManager.getInstance().getServerBinding());
		params.put("expressions", "isusing = '1'");
		BeanRequest<List<CoworkerEntity>> request = new BeanRequest<List<CoworkerEntity>>() {
		};
		request.setResultListener(listener);
		Log.d("---url",UrlManager.getInterfaceUrl()+"?serviceName=pageQuery&pageNUM=1"+"&pageSize=800"+"&objectApiName=ccuser"+"&binding="+
				RunTimeManager.getInstance().getServerBinding()+"&expressions=isusing = '1'");
		sendGetSync(UrlManager.getInterfaceUrl(), params, request);
	}

	@Override
	public void getContactList(ResultCallBack<List<CoworkerEntity>> listener) {
		String px = "isusing = '1'";
		String a = px;
		try {
			a = URLEncoder.encode(px, "utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		String url = UrlManager.getInterfaceUrl()
				+ "?serviceName=cquery&objectApiName=ccuser&expressions=" + a
				+ "&binding=" + RunTimeManager.getInstance().getServerBinding();
		Log.d("---url",url);
		BeanRequest<List<CoworkerEntity>> request = new BeanRequest<List<CoworkerEntity>>() {
		};
		request.setResultListener(listener);
		sendGet(url, request);
	}

	@Override
	public void getContactList(int page, int pageNum, CoworkerListEvent event) {
		EventRequest<List<CoworkerEntity>> request = new EventRequest<List<CoworkerEntity>>() {
		};
		request.setEvent(event);
		RequestParams params = new RequestParams();
		params.put("serviceName", "pageQuery");
		params.put("pageNUM", page);
		params.put("pageSize", pageNum);
		params.put("objectApiName", "ccuser");
		params.put("binding", RunTimeManager.getInstance().getServerBinding());// ,c.ismefollow,c.isfollowme
		params.put("expressions", "isusing = '1'");
		Log.d("---url",UrlManager.getInterfaceUrl()+"?serviceName=pageQuery&pageNUM="+page+"&pageSize="+pageNum+"&objectApiName=ccuser"+"&binding="+
				RunTimeManager.getInstance().getServerBinding()+"&expressions=isusing = '1'");
		sendGet(UrlManager.getInterfaceUrl(), params, request);
	}

	@Override
	public void getContactListNew(String code, CoworkerListEvent event) {
		// TODO Auto-generated method stub
		EventRequest<List<CoworkerEntity>> request = new EventRequest<List<CoworkerEntity>>() {
		};
		request.setEvent(event);
		RequestParams params = new RequestParams();
		params.put("serviceName", "getUserInfoList");
		params.put("code", code);
		Log.d("---url",UrlManager.getInterfaceUrl()+"?serviceName=getUserInfoList&pageNUM="+"&code=code"+"&binding="+
				RunTimeManager.getInstance().getServerBinding());
		params.put("binding", RunTimeManager.getInstance().getServerBinding());// ,c.ismefollow,c.isfollowme
		sendGet(UrlManager.getInterfaceUrl(), params, request);
	}
	@Override
	public void getContactListGuanzhu(CoworkerListEvent event) {
		// TODO Auto-generated method stub
		EventRequest<List<CoworkerEntity>> request = new EventRequest<List<CoworkerEntity>>() {
		};
		request.setEvent(event);
		RequestParams params = new RequestParams();
		params.put("serviceName", "getMeFollowList");
		params.put("binding", RunTimeManager.getInstance().getServerBinding());// ,c.ismefollow,c.isfollowme
		sendGet(UrlManager.getInterfaceUrl(), params, request);
	}
	

	@Override
	public void getUserInfoChatter(String userId,
			ResultCallBack<ChatUser> listener) {
		UserInfoParam param = new UserInfoParam();
		param.isReturnChatter = false;
		param.userId = userId;
		BeanRequest<ChatUser> request = new BeanRequest<ChatUser>() {
		};
		request.setResultListener(listener);
		sendPost(param, request, "getChatterUserInfo", true);
	}

	@Override
	public void followContact(OperateFollowRelation operation,
			RequestListener listener) {
		JsonObjectRequest request = new JsonObjectRequest();
		request.setRequestListener(listener);
		sendPost(operation, request, "operateFollowRelation", true);
	}

	// 备份 getContactUserInfo
	private void getContactUserInfo2(String userId,
			ResultCallBack<List<User>> listener) {
		String exp = "id=";
		String exp2 = "'" + userId + "'";
		try {
			exp2 = URLEncoder.encode(exp2, "utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		String url = UrlManager.getInterfaceUrl()
				+ "?serviceName=cquery&objectApiName=ccuser&expressions=" + exp
				+ exp2 + "&binding="
				+ RunTimeManager.getInstance().getServerBinding();
		BeanRequest<List<User>> request = new BeanRequest<List<User>>() {
		};
		request.setResultListener(listener);
		sendGet(url, request);
	}
	@Override
	public void getSubContacts(CoworkerListEvent event) {
		EventRequest<List<CoworkerEntity>> request = new EventRequest<List<CoworkerEntity>>() {
		};
		request.setEvent(event);
		sendPost(null, "getSubordinates", request);
	}
	@Override
	public void getSubContactsGZ(CoworkerListEvent event) {
		EventRequest<List<CoworkerEntity>> request = new EventRequest<List<CoworkerEntity>>() {
		};
		request.setEvent(event);
		sendPost(null, "getMeFollowList", request);
	}


}
